package com.scaffold.config.swagger;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;


/**
 * @author Da Shuai
 * @since 2019-11-19 10:27
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Value("${spring.swagger.enable:false}")
    private Boolean enable;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(enable)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.scaffold.controller"))
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
    }

    /**
     * api基本信息
     *
     * @return ApiInfo
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("日事清-OKR")
                .description("目标应用API文档")
                .license("噢力给")
                .termsOfServiceUrl("https://www.rishiqing.com")
                .version("v9")
                .contact(new Contact("", "", "you@your-company.com"))
                .build();
    }

    /**
     * 配置全局参数：securitySchemes的ApiKey中增加一个名为token,type为header的参数。
     *
     * @return List<ApiKey>
     */
    private List<ApiKey> securitySchemes() {
        List<ApiKey> list = new ArrayList<ApiKey>();
        list.add(new ApiKey("token", "token", "header"));
        return list;
    }

    /**
     * 通过正则表达式，去除掉不需要使用参数的接口，所有包含"auth"的接口不需要使用上文中设置的名为“token”，type为“header”的参数。
     *
     * @return List<SecurityContext>
     */
    private List<SecurityContext> securityContexts() {
        List<SecurityContext> list = new ArrayList<SecurityContext>();
        list.add(SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex("^(?!auth).*$"))
                .build());
        return list;
    }

    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        List<SecurityReference> list = new ArrayList<SecurityReference>();
        list.add(new SecurityReference("token", authorizationScopes));
        return list;
    }
}
